package com.white.utilkit

import android.app.Activity
import android.content.Context
import android.graphics.Paint
import android.graphics.Point
import android.graphics.RectF
import android.os.Build
import android.text.InputFilter
import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.util.DisplayMetrics
import android.view.Display
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.StyleRes
import androidx.core.content.ContextCompat
import java.util.*
import java.util.regex.Pattern


/**
 * Created by ytf on 2018/3/23 023.
 * Description:
 */

object UiKit {

    fun backgroundAlpha(ac: Activity, bgAlpha: Float) {
        val lp = ac.window.attributes
        lp.alpha = bgAlpha
        ac.window.attributes = lp
    }

    fun getScreenSize(ctx: Activity): Pair<Int, Int> {

        val w = ctx.windowManager
        val display = w.defaultDisplay
        val metrics = DisplayMetrics()
        display.getMetrics(metrics)
        // since SDK_INT = 1;
        var widthPixels = metrics.widthPixels
        var heightPixels = metrics.heightPixels
        // includes window decorations (statusbar bar/menu bar)
        if (Build.VERSION.SDK_INT in 14..16) {
            try {
                widthPixels = Display::class.java.getMethod("getRawWidth").invoke(display) as Int
                heightPixels = Display::class.java.getMethod("getRawHeight").invoke(display) as Int
            } catch (ignored: Exception) {
            }
        }

        // includes window decorations (statusbar bar/menu bar)
        if (Build.VERSION.SDK_INT >= 17) {
            try {
                val realSize = Point()
                Display::class.java.getMethod("getRealSize", Point::class.java).invoke(display, realSize)
                widthPixels = realSize.x
                heightPixels = realSize.y
            } catch (ignored: Exception) {
            }
        }
        return Pair(widthPixels, heightPixels)
    }

    fun screenHeightDp(ctx: Activity): Int {

        val dm = ctx.resources.displayMetrics

        return (getScreenSize(ctx).second * 1.0f / dm.density + .5f).toInt()
    }

    fun screenWidthDp(ctx: Activity): Int {
        val dm = ctx.resources.displayMetrics
        return (getScreenSize(ctx).first * 1.0f / dm.density + .5f).toInt()
    }

    fun hideKeyboard(activity: Activity) {
        val im = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        if (im != null) {
            val focus = activity.currentFocus
            if (focus != null) {
                im.hideSoftInputFromWindow(
                    focus.applicationWindowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        }
    }

    fun screenHeight(ctx: Context): Int {
        return ctx.resources.displayMetrics.heightPixels
    }

    fun screenWidth(ctx: Context): Int {
        return ctx.resources.displayMetrics.widthPixels
    }
    /***
     *
     * @return 获得状态栏高度
     */
    fun getStatusHight(ctx: Context): Int {
        val identifyId = ctx.resources.getIdentifier("status_bar_height", "dimen", "android")
        return if (identifyId > 0) {
            ctx.resources.getDimensionPixelSize(identifyId)
        } else 0
    }

    fun dpToPx(context: Context, dp: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dp * scale + 0.5).toInt()
    }

    fun pxToDp(context: Context, px: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (px / scale + 0.5).toInt()
    }


    fun getColor(ctx: Context, @ColorRes colorId: Int): Int {
        return ContextCompat.getColor(ctx, colorId)
    }

    fun gradientColor(alpha: Int, color: Int): Int {
        return alpha and 0xff shl 24 or (color and 0x00ffffff)
    }


    fun calcTextSuitBaseY(rectF: RectF, paint: Paint): Float {
        return rectF.top + rectF.height() / 2 -
                (paint.fontMetrics.ascent + paint.fontMetrics.descent) / 2
    }

    fun forbidEmoji(et: EditText?) {
        if (et != null) {
            val emoji = Pattern
                .compile(
                    "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\ud83e\udd00-\ud83e\udfff]|[\u2600-\u27ff]",
                    Pattern.UNICODE_CASE or Pattern.CASE_INSENSITIVE
                )
            var filters = et.filters
            filters = Arrays.copyOf(filters, filters.size + 1)
            filters[filters.size - 1] =
                InputFilter { source, start, end, dest, dstart, dend ->
                    val emojiMatcher = emoji.matcher(source)
                    if (emojiMatcher.find()) {
                        ""
                    } else source
                }
            et.filters = filters
        }
    }
}
